package com.woniuxy.joylifemarket.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.common.entity.*;
import com.woniuxy.joylifemarket.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class LeaveMessageController {
    @Autowired
    MarketLeaveMessageService marketLeaveMessageService;

    //加载商品留言
    @RequestMapping("/initleavemessage")
    public Object leaveMessage(String commodityid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<MarketLeavemessage> allLeaveMessages = marketLeaveMessageService.findAllLeaveMessageByCommodityId(commodityid);
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", allLeaveMessages);
//            System.out.println(allLeaveMessages);
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败");
            e.printStackTrace();
            return result;
        }
    }

    //发送商品留言
    @RequestMapping("/putleavemessage")
    public Object putLeaveMessage(String putleavemessage) {
        Map<String, Object> result = new HashMap<>();
        MarketLeavemessage marketLeavemessage = JSON.parseObject(putleavemessage, MarketLeavemessage.class);
//        System.out.println(marketLeavemessage);
        try {
            int i = marketLeaveMessageService.insertLeaveMessage(marketLeavemessage);
            if (i==0) {
                result.put("code", 500);
                result.put("msg", "插入失败");
                return result;
            }
            result.put("code", 200);
            result.put("msg", "插入成功");
            return result;
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "插入失败");
            e.printStackTrace();
            return result;
        }
    }
}
